package com.example.casper.Experiment2024.share;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.casper.Experiment2024.R;
import com.example.casper.Experiment2024.items.Location;

import java.util.List;

public class LocationAdapter extends RecyclerView.Adapter<LocationAdapter.LocationViewHolder> {

    private final Context context;
    private final List<Location> locationList;

    public LocationAdapter(Context context, List<Location> locationList) {
        this.context = context;
        this.locationList = locationList;
    }

    @NonNull
    @Override
    public LocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new LocationViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LocationViewHolder holder, int position) {
        Location location = locationList.get(position);

        // 显示数据
        Glide.with(holder.itemView.getContext()).load(location.getPicPath()).into(holder.imageViewPic);
        holder.textViewLocationName.setText(location.getLocationName());
        holder.textViewLatitude.setText(String.valueOf(location.getLatitude()));
        holder.textViewLongitude.setText(String.valueOf(location.getLongitude()));
        holder.textViewFeeling.setText(location.getFeeling());

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, shareLocationsDetailActivity.class);

            // 将 Location 对象拆分为基本数据类型
            intent.putExtra("locationName", location.getLocationName());
            intent.putExtra("latitude", location.getLatitude());
            intent.putExtra("longitude", location.getLongitude());
            intent.putExtra("feeling", location.getFeeling());
            intent.putExtra("province", location.getProvince());
            intent.putExtra("picPath", location.getPicPath() != null ? location.getPicPath().toString() : null);

            context.startActivity(intent);
        });
    }

    @Override
    public int getItemCount() {
        return locationList.size();
    }

    public static class LocationViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewPic;
        TextView textViewLocationName, textViewLatitude, textViewLongitude, textViewFeeling;

        public LocationViewHolder(@NonNull View itemView) {
            super(itemView);
            imageViewPic = itemView.findViewById(R.id.image_view_pic);
            textViewLocationName = itemView.findViewById(R.id.text_view_location_name);
            textViewLatitude = itemView.findViewById(R.id.text_view_latitude);
            textViewLongitude = itemView.findViewById(R.id.text_view_longitude);
            textViewFeeling = itemView.findViewById(R.id.text_view_feeling);
        }
    }
}
